<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JSDoc: Source: utils/snapshot.js</title>

    <script src="scripts/prettify/prettify.js"> </script>
    <script src="scripts/prettify/lang-css.js"> </script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>

<body>

<div id="main">

    <h1 class="page-title">Source: utils/snapshot.js</h1>

    



    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>const Request = require('request')
const Util = require('./util')

/**
 * @class
 * Provide Snapshot functionaity for cameras.
 * &lt;h3>Functions&lt;/h3>
 * {@link Snapshot#getSnapshot},
 * &lt;br>&lt;br>
 * &lt;h3>Overview&lt;/h3>
 * Use the Snapshot::getSnapshot() method to retrieve a snapshot from the ONVIF-compliant camera.
 */
class Snapshot {
  constructor () {
    this.snapshotUri = ''
    this.username = ''
    this.password = ''
  }

  init (snapshotUri, username, password) {
    this.snapshotUri = snapshotUri
    this.username = username
    this.password = password
  }

  /**
   * Retrieves a snapshot from the specified camera
   * @param {callback=} callback Optional callback, instead of a Promise.
   * @example
   * const OnvifManager = require('onvif-nvt')
   * OnvifManager.connect('10.10.1.60', 80, 'username', 'password')
   *   .then(results => {
   *     let camera = results
   *     // calling add method will automatically initialize snapshot
   *     // with the defaultProfile's snapshotUri
   *     camera.add('snapshot')
   *     camera.snapshot.getSnapshot()
   *       .then(results => {
   *         let mimeType = results.mimeType
   *         let rawImage = results.image
   *         let prefix = 'data:' + mimeType + ';base64,'
   *         let base64Image = Buffer.from(rawImage, 'binary').toString('base64')
   *         let image = prefix + base64Image
   *         // 'image' is now ready to be displayed on a web page
   *         // ...
   *       })
   *     }
   *   })
   */
  getSnapshot (callback) {
    let promise = new Promise((resolve, reject) => {
      Request({
        method: 'GET',
        uri: this.snapshotUri,
        gzip: true,
        encoding: 'binary',
        'auth': {
          'user': this.username,
          'pass': this.password,
          'sendImmediately': false
        }
      },
      (error, response, body) => {
        if (error) {
          reject(error)
        }
        const mimeType = response.headers['content-type']
        const data = {
          mimeType: mimeType,
          image: Buffer.from(body, 'binary')
        }
        resolve(data)
      })
    })
    if (Util.isValidCallback(callback)) {
      promise.then(results => {
        callback(null, results)
      }).catch(error => {
        callback(error)
      })
    }
    else {
      return promise
    }
  }
}

module.exports = Snapshot
</code></pre>
        </article>
    </section>




</div>

<nav>
    <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Access.html">Access</a></li><li><a href="AccessRules.html">AccessRules</a></li><li><a href="Action.html">Action</a></li><li><a href="Analytics.html">Analytics</a></li><li><a href="Camera.html">Camera</a></li><li><a href="Core.html">Core</a></li><li><a href="Credential.html">Credential</a></li><li><a href="DeviceIO.html">DeviceIO</a></li><li><a href="Discovery.html">Discovery</a></li><li><a href="Display.html">Display</a></li><li><a href="Door.html">Door</a></li><li><a href="Events.html">Events</a></li><li><a href="Imaging.html">Imaging</a></li><li><a href="Media.html">Media</a></li><li><a href="Media2.html">Media2</a></li><li><a href="OnvifManager.html">OnvifManager</a></li><li><a href="Ptz.html">Ptz</a></li><li><a href="Receiver.html">Receiver</a></li><li><a href="Recording.html">Recording</a></li><li><a href="Replay.html">Replay</a></li><li><a href="Schedule.html">Schedule</a></li><li><a href="Search.html">Search</a></li><li><a href="Security.html">Security</a></li><li><a href="Snapshot.html">Snapshot</a></li><li><a href="Soap.html">Soap</a></li><li><a href="Thermal.html">Thermal</a></li><li><a href="VideoAnalytics.html">VideoAnalytics</a></li></ul><h3><a href="global.html">Global</a></h3>
</nav>

<br class="clear">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Tue Feb 27 2018 14:01:19 GMT-0700 (MST)
</footer>

<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>
